package com.example.fw;

import java.util.List;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public abstract class HelperBase 
{
	protected ApplicationManager manager;
	private WebDriver driver;
	public boolean acceptNextAlert = true;

	public HelperBase(ApplicationManager manager) {
		this.manager = manager;
		this.driver = manager.driver;
	}
	
	public boolean isAlertPresent() {
	    try {
	      driver.switchTo().alert();
	      return true;
	    } catch (NoAlertPresentException e) {
	      return false;
	    }
	  }

	public String closeAlertAndGetItsText() {
	    try {
	      Alert alert = driver.switchTo().alert();
	      String alertText = alert.getText();
	      if (acceptNextAlert) {
	        alert.accept();
	      } else {
	        alert.dismiss();
	      }
	      return alertText;
	    } finally {
	      acceptNextAlert = true;
	    }
	  }

	protected void type(By locator, String text) {
		if (text != null) {
			findElement(locator).clear();
			findElement(locator).sendKeys(text);		
		}

	}

	protected void click(By by) {
		findElement(by).click();
	}
	
	protected void selectByText(By by, String text) {
		if (text != null)
			new Select(findElement(by)).selectByVisibleText(text);
	}

	protected WebElement findElement(By by) {
		return driver.findElement(by);
	}
	
	protected List<WebElement> findElements(By by) {
		return driver.findElements(by);
	}
	
	protected String getCurrentUrl() {
		return driver.getCurrentUrl();
	}
	
	protected boolean isElementPresented(By by) {
		return findElements(by).size() > 0;
	}
	
	protected boolean isElementPresent(By by) {
	    try {
	      findElement(by);
	      return true;
	    } catch (NoSuchElementException e) {
	      return false;
	    }
	}	
}


